UIPanelWindows["MyQuestsWizard_Parent"] = { area = "left", pushable = 3 };

MyQuests.MaxWizardPages = 8;
MyQuests.CurrentWizardPage = 1;

MyQuests.Dewdrop = AceLibrary("Dewdrop-2.0")

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:LoadWizard()
  MyQuests:Debug("LoadWizard()");
  
  MQWizard_TitleText:SetText(MQL["MyQuests Wizard"]);
  MQWizard_StepNumberText:SetText(string.format(MQL["Wizard Step"], MyQuests.CurrentWizardPage, MyQuests.MaxWizardPages));

  MQWizard_Page1Title:SetText(MQL["Quest Options"]);
  
  MQWizard_CreateTitleLabel:SetText(MQL["Quest Title"]);
  MQWizard_CreateLevelLabel:SetText(MQL["Quest Level"]);
  MQWizard_CreateChainTitle:SetText(MQL["Chain Parent"]);
  
  MQWizard_FailureDeathToggleLabel:SetText(MQL["Player Death"]);
  MQWizard_FailureTimeToggleLabel:SetText(MQL["Time Limit"]);
  MQWizard_FailureTimeEntryLabel:SetText(MQL["minutes"]);
  
  MQWizard_CompleteRepeatableToggleLabel:SetText(MQL["Repeatable Quest"]);
  MQWizard_CompleteLocalStoreToggleLabel:SetText(MQL["Local History"]);
  
  MQWizard_Page2Title:SetText(MQL["Filter Options"]);

  -- Description header pulled from WoW
  
  MyQuestsWizard_Page3ContentInfo:SetText(MQL["Desc Info"]);
  
  MyQuestsWizard_Page4ContentHeader:SetText(MQL["Summary"]);
  MyQuestsWizard_Page4ContentInfo:SetText(MQL["Summary Info"]);

  MQWizard_ObjectivesInfo:SetText(MQL["Objective Info"]);

  for i=1, 4, 1 do
    getglobal("MQWizard_Objective" .. i .. "NameLabel"):SetText(MQL["Name"]);
    getglobal("MQWizard_Objective" .. i .. "CountLabel"):SetText(MQL["Count"]);
    getglobal("MQWizard_Objective" .. i .. "LevelLabel"):SetText(MQL["Levels"]);
    getglobal("MQWizard_Objective" .. i .. "LocationLabel"):SetText(MQL["Location"]);
    getglobal("MQWizard_Objective" .. i .. "CoordLabel"):SetText(MQL["Coordinates"]);
    
    getglobal("MQWizard_Objective" .. i .. "ShowLeaderLabel"):SetText(MQL["Show Leader"]);
  end
  
  MQWizard_RewardLabel:SetText(MQL["Reward Label"]);
  MQWizard_GuildRewardLabel:SetText(MQL["Guild Action"]);
  MQWizard_RewardGuildInviteLabel:SetText(MQL["Invitation"]);
  MQWizard_RewardGuildPromoteLabel:SetText(MQL["Promotion"]);
  
  MQWizard_RewardChoiceLabel:SetText(MQL["Reward Choice Label"]);
  MQWizard_GuildRewardChoiceLabel:SetText(MQL["Guild Action"]);
  MQWizard_RewardChoiceGuildInviteLabel:SetText(MQL["Invitation"]);
  MQWizard_RewardChoiceGuildPromoteLabel:SetText(MQL["Promotion"]);
  
  MyQuestsWizard_Page7ContentHeader:SetText(MQL["Progress"]);
  MyQuestsWizard_Page7ContentInfo:SetText(MQL["Progress Info"]);
  
  MyQuestsWizard_Page8ContentInfo:SetText(MQL["Complete Info"]);


  MyQuests:ObjectiveTypeInitialize();
  
  -- initialize the drop down functions
  MQWizard_Objective1Type.ToggleMenu = function()
    if (MyQuests.Dewdrop:IsOpen(MQWizard_Objective1Type)) then
      MyQuests.Dewdrop:Close();
    else
      MyQuests.Dewdrop:Open(MQWizard_Objective1Type);
      PlaySound("igMainMenuOptionCheckBoxOn");
    end
  end
  MQWizard_Objective2Type.ToggleMenu = function()
    if (MyQuests.Dewdrop:IsOpen(MQWizard_Objective2Type)) then
      MyQuests.Dewdrop:Close();
    else
      MyQuests.Dewdrop:Open(MQWizard_Objective2Type);
      PlaySound("igMainMenuOptionCheckBoxOn");
    end
  end
  MQWizard_Objective3Type.ToggleMenu = function()
    if (MyQuests.Dewdrop:IsOpen(MQWizard_Objective3Type)) then
      MyQuests.Dewdrop:Close();
    else
      MyQuests.Dewdrop:Open(MQWizard_Objective3Type);
      PlaySound("igMainMenuOptionCheckBoxOn");
    end
  end
  MQWizard_Objective4Type.ToggleMenu = function()
    if (MyQuests.Dewdrop:IsOpen(MQWizard_Objective4Type)) then
      MyQuests.Dewdrop:Close();
    else
      MyQuests.Dewdrop:Open(MQWizard_Objective4Type);
      PlaySound("igMainMenuOptionCheckBoxOn");
    end
  end

  
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:ShowWizard()

  -- clear contents if no edit

  ShowUIPanel(MyQuestsWizard_Parent);

  -- reset to page 1
  for i=1, MyQuests.MaxWizardPages, 1 do
    HideUIPanel(getglobal("MyQuestsWizard_Page" .. i));
  end
  ShowUIPanel(MyQuestsWizard_Page1);
  MyQuests.CurrentWizardPage = 1;
  MQWizard_StepNumberText:SetText(string.format(MQL["Wizard Step"], MyQuests.CurrentWizardPage, MyQuests.MaxWizardPages));

end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:PrevWizardPage()
  HideUIPanel(getglobal("MyQuestsWizard_Page" .. MyQuests.CurrentWizardPage));
  MyQuests.CurrentWizardPage = MyQuests.CurrentWizardPage - 1;
  ShowUIPanel(getglobal("MyQuestsWizard_Page" .. MyQuests.CurrentWizardPage));
  
  if (MyQuests.CurrentWizardPage == 1) then
    MQWizard_PrevButton:Disable();
  else
    MQWizard_PrevButton:Enable();    
  end
  MQWizard_NextButton:Enable();

  MQWizard_StepNumberText:SetText(string.format(MQL["Wizard Step"], MyQuests.CurrentWizardPage, MyQuests.MaxWizardPages));
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:NextWizardPage()
  HideUIPanel(getglobal("MyQuestsWizard_Page" .. MyQuests.CurrentWizardPage));
  MyQuests.CurrentWizardPage = MyQuests.CurrentWizardPage + 1;
  ShowUIPanel(getglobal("MyQuestsWizard_Page" .. MyQuests.CurrentWizardPage));  

  if (MyQuests.CurrentWizardPage == MyQuests.MaxWizardPages) then
    MQWizard_NextButton:Disable();
  else
    MQWizard_NextButton:Enable();    
  end
  MQWizard_PrevButton:Enable();

  MQWizard_StepNumberText:SetText(string.format(MQL["Wizard Step"], MyQuests.CurrentWizardPage, MyQuests.MaxWizardPages));
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:CancelWizard()
  HideUIPanel(MyQuestsWizard_Parent);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:SaveWizard()
  local tmpQuest;
  local updating;

  if (MyQuestsWizard_Parent.quest) then
    tmpQuest = MyQuestsWizard_Parent.quest;
    updating = true;
  else
    tmpQuest = {};
    updating = false;
  end
  
  -- save fields to quest table

  if (not updating) then
    tmpQuest.id = UnitName("player") .. time();
    MyQuests:Debug("setting id: " .. tmpQuest.id);
  end

  
  tmpQuest.title = MQWizard_CreateTitleEntry:GetText();
  tmpQuest.level = tonumber(MQWizard_CreateLevelEntry:GetText());
  if (tmpQuest.level == nil) then
    tmpQuest.level = 0;
  end
  
  tmpQuest.finishPlayer = UnitName("player");
  
  --tmpQuest.parent = UIDropDownMenu_GetSelectedValue(MQWizard_ChainDropDown);
  
  tmpQuest.failOnDeath = MQWizard_FailureDeathToggle:GetChecked();
  
  if (MQWizard_FailureTimeToggle:GetChecked()) then
    tmpQuest.timeLeft = tonumber(MQWizard_FailureTimeToggle:GetText()) * 60;
  else
    tmpQuest.timeLeft = nil;
  end
  
  tmpQuest.isRepeatable = MQWizard_CompleteRepeatableToggle:GetChecked();
  tmpQuest.localStore = MQWizard_CompleteLocalStoreToggle:GetChecked();

  -- filters
  
  tmpQuest.descText = MQWizard_DescEntry:GetText();

  tmpQuest.summaryText = MQWizard_SummaryEntry:GetText();

  -- objectives
  tmpQuest.objectives = {};
  for i=1, 4, 1 do
    local refStr = "MQWizard_Objective" .. i;
    local objType = getglobal(refStr .. "Type")._svalue;

    if (objType ~= nil and objType ~= "None") then
      local objOut = {};
      MyQuests.QuestObjectives[objType]:OnCreateOrEdit(refStr, objOut);
      table.insert(tmpQuest.objectives, objOut);
    end
  end
  
  -- rewards
  tmpQuest.rewardMoney = MoneyInputFrame_GetCopper(MQWizard_RewardMoney);
  for i=1, 4, 1 do
    -- look through the type of rewards and choices.
  end

  -- reward choices
  for i=1, 4, 1 do
    -- look through the type of rewards and choices.
  end
  
  tmpQuest.progressText = MQWizard_ProgressEntry:GetText();

  tmpQuest.completeText = MQWizard_CompleteEntry:GetText();
  
  if (updating == false) then
    table.insert(self.db.char.QuestDiary, tmpQuest);
  end
  
  -- clear and close out the wizard
  self:CancelWizard();
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:ObjectiveTypeInitialize()

  -- sort the objectives table
  self.t2 = {};
  table.foreach (MyQuests.ObjectiveTypes, function (j) table.insert (self.t2, j) end )
  table.sort(self.t2);


  -- initialize the drop down values  
  MyQuests.Dewdrop:Register(MQWizard_Objective1Type,
    'point', "TOPLEFT",
    'relativePoint', "BOTTOMLEFT",
    'children', function()
      MyQuests.Dewdrop:AddLine(
        'text', MQL["None"],
        'closeWhenClicked', true,
        'tooltipTitle', "",
        'func', function()
          MQWizard_Objective1Type._svalue = "None";
          MQWizard_Objective1TypeSelection:SetText(MQL["None"]);
          MyQuests:ObjectiveTypeOnSelection(MQWizard_Objective1Type)
          PlaySound("igMainMenuOptionCheckBoxOn");
        end
      )
      for k, v in pairs(self.t2) do
        MyQuests.Dewdrop:AddLine(
          'text', MQL[v],
          'closeWhenClicked', true,
          'tooltipTitle', "",
          'func', function()
            MQWizard_Objective1Type._svalue = v;
            MQWizard_Objective1TypeSelection:SetText(MQL[v]);
            MyQuests:ObjectiveTypeOnSelection(MQWizard_Objective1Type)
            PlaySound("igMainMenuOptionCheckBoxOn");
          end
        )
      end
    end
  );

  MyQuests.Dewdrop:Register(MQWizard_Objective2Type,
    'point', "TOPLEFT",
    'relativePoint', "BOTTOMLEFT",
    'children', function()
      MyQuests.Dewdrop:AddLine(
        'text', MQL["None"],
        'closeWhenClicked', true,
        'tooltipTitle', "",
        'func', function()
          MQWizard_Objective2Type._svalue = "None";
          MQWizard_Objective2TypeSelection:SetText(MQL["None"]);
          MyQuests:ObjectiveTypeOnSelection(MQWizard_Objective2Type)
          PlaySound("igMainMenuOptionCheckBoxOn");
        end
      )
      for k, v in pairs(self.t2) do
        MyQuests.Dewdrop:AddLine(
          'text', MQL[v],
          'closeWhenClicked', true,
          'tooltipTitle', "",
          'func', function()
            MQWizard_Objective2Type._svalue = v;
            MQWizard_Objective2TypeSelection:SetText(MQL[v]);
            MyQuests:ObjectiveTypeOnSelection(MQWizard_Objective2Type)
            PlaySound("igMainMenuOptionCheckBoxOn");
          end
        )
      end
    end
  );

  MyQuests.Dewdrop:Register(MQWizard_Objective3Type,
    'point', "TOPLEFT",
    'relativePoint', "BOTTOMLEFT",
    'children', function()
      MyQuests.Dewdrop:AddLine(
        'text', MQL["None"],
        'closeWhenClicked', true,
        'tooltipTitle', "",
        'func', function()
          MQWizard_Objective3Type._svalue = "None";
          MQWizard_Objective3TypeSelection:SetText(MQL["None"]);
          MyQuests:ObjectiveTypeOnSelection(MQWizard_Objective3Type)
          PlaySound("igMainMenuOptionCheckBoxOn");
        end
      )
      for k, v in pairs(self.t2) do
        MyQuests.Dewdrop:AddLine(
          'text', MQL[v],
          'closeWhenClicked', true,
          'tooltipTitle', "",
          'func', function()
            MQWizard_Objective3Type._svalue = v;
            MQWizard_Objective3TypeSelection:SetText(MQL[v]);
            MyQuests:ObjectiveTypeOnSelection(MQWizard_Objective3Type)
            PlaySound("igMainMenuOptionCheckBoxOn");
          end
        )
      end
    end
  );

  MyQuests.Dewdrop:Register(MQWizard_Objective4Type,
    'point', "TOPLEFT",
    'relativePoint', "BOTTOMLEFT",
    'children', function()
      MyQuests.Dewdrop:AddLine(
        'text', MQL["None"],
        'closeWhenClicked', true,
        'tooltipTitle', "",
        'func', function()
          MQWizard_Objective4Type._svalue = "None";
          MQWizard_Objective4TypeSelection:SetText(MQL["None"]);
          MyQuests:ObjectiveTypeOnSelection(MQWizard_Objective4Type)
          PlaySound("igMainMenuOptionCheckBoxOn");
        end
      )
      for k, v in pairs(self.t2) do
        MyQuests.Dewdrop:AddLine(
          'text', MQL[v],
          'closeWhenClicked', true,
          'tooltipTitle', "",
          'func', function()
            MQWizard_Objective4Type._svalue = v;
            MQWizard_Objective4TypeSelection:SetText(MQL[v]);
            MyQuests:ObjectiveTypeOnSelection(MQWizard_Objective4Type)
            PlaySound("igMainMenuOptionCheckBoxOn");
          end
        )
      end
    end
  );

  -- set the default to "None"
  MQWizard_Objective1Type._svalue = nil;
  MQWizard_Objective1TypeSelection:SetText(MQL["None"]);
  MQWizard_Objective2Type._svalue = nil;
  MQWizard_Objective2TypeSelection:SetText(MQL["None"]);
  MQWizard_Objective3Type._svalue = nil;
  MQWizard_Objective3TypeSelection:SetText(MQL["None"]);
  MQWizard_Objective4Type._svalue = nil;
  MQWizard_Objective4TypeSelection:SetText(MQL["None"]);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:ObjectiveTypeOnSelection(ddRef)
  local frameRef = ddRef:GetParent();
  
  MyQuests:Print(frameRef);
  
  -- hide/show items based on selection
  if (ddRef._svalue == 1) then
  
  end
end


--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:PlaceWizardRewardItem()
  if(CursorHasItem() and MyQuests.CursorBypassItem) then
    SetItemButtonTexture(this, MyQuests.CursorBypassItem.texture);
    SetItemButtonCount(this, MyQuests.CursorBypassItem.count);

    -- extract the item name from the link and string the brackets
    local tmpName = string.gsub(MyQuests.CursorBypassItem.link, "|cff(.*)|H(.*)|h(.*)|h|r.*", "%3");
    tmpName = string.sub(tmpName, 2, string.len(tmpName)-1);
    
    getglobal(this:GetName().."Name"):SetText(tmpName);
    getglobal(this:GetName().."DeleteButton"):Show();

    this.item = {}
    this.item.name = tmpName;
    this.item.link = MyQuests.CursorBypassItem.link;
    this.item.texture = MyQuests.CursorBypassItem.texture;
    this.item.isUseable = MyQuests.CursorBypassItem.isUseable;

    PickupSpell(1, "spell"); -- Doing this twice is a safe way to clear cursor
    PickupSpell(1, "spell");

    MyQuests.CursorBypassItem = nil;
  end
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:DeleteWizardRewardItem()
  this:GetParent().item = nil;
  getglobal(this:GetParent():GetName().."Name"):SetText(MQL["Drop Item Here"]);
  getglobal(this:GetParent():GetName().."DeleteButton"):Hide();
  
  SetItemButtonTexture(this:GetParent(), nil);
  SetItemButtonCount(this:GetParent(), nil);
end

function MyQuests:RewardRadioButton_OnClick(index)
  -- will return the state of the button we are chaning to
  
  if (index == 1) then
    if (MQWizard_RewardGuildNone:GetChecked()) then
      MyQuestsWizard_Page6ContentRewardItem1Name:SetText(MQL["Drop Item Here"]);
      SetItemButtonTexture(MyQuestsWizard_Page6ContentRewardItem1, nil);
      SetItemButtonCount(MyQuestsWizard_Page6ContentRewardItem1, nil);
      MyQuestsWizard_Page6ContentRewardItem1DeleteButton:Hide();
    end
    
    MQWizard_RewardGuildNone:SetChecked(1);
    MQWizard_RewardGuildInvite:SetChecked(nil);
    MQWizard_RewardGuildPromote:SetChecked(nil);
  elseif (index == 2) then
    MQWizard_RewardGuildNone:SetChecked(nil);
    --MQWizard_RewardGuildInvite:SetChecked(1);
    MQWizard_RewardGuildPromote:SetChecked(nil);
    MyQuestsWizard_Page6ContentRewardItem1Name:SetText(MQL["Guild Invitation"]);
    SetItemButtonTexture(MyQuestsWizard_Page6ContentRewardItem1, "Interface\\Icons\\INV_Misc_Note_02");
  elseif (index == 3) then
    MQWizard_RewardGuildNone:SetChecked(nil);
    MQWizard_RewardGuildInvite:SetChecked(nil);
    --MQWizard_RewardGuildPromote:SetChecked(1);
    MyQuestsWizard_Page6ContentRewardItem1Name:SetText(MQL["Guild Promotion"]);
    SetItemButtonTexture(MyQuestsWizard_Page6ContentRewardItem1, "Interface\\Icons\\INV_Misc_Note_02");
  elseif (index == 4) then
    if (MQWizard_RewardChoiceGuildNone:GetChecked()) then
      MyQuestsWizard_Page6ContentRewardChoiceItem1Name:SetText(MQL["Drop Item Here"]);
      SetItemButtonTexture(MyQuestsWizard_Page6ContentRewardChoiceItem1, nil);
      SetItemButtonCount(MyQuestsWizard_Page6ContentRewardChoiceItem1, nil);
      MyQuestsWizard_Page6ContentRewardChoiceItem1DeleteButton:Hide();
    end

    MQWizard_RewardChoiceGuildNone:SetChecked(1);
    MQWizard_RewardChoiceGuildInvite:SetChecked(nil);
    MQWizard_RewardChoiceGuildPromote:SetChecked(nil);
  elseif (index == 5) then
    MQWizard_RewardChoiceGuildNone:SetChecked(nil);
    --MQWizard_RewardChoiceGuildInvite:SetChecked(1);
    MQWizard_RewardChoiceGuildPromote:SetChecked(nil);
    MyQuestsWizard_Page6ContentRewardChoiceItem1Name:SetText(MQL["Guild Invitation"]);
    SetItemButtonTexture(MyQuestsWizard_Page6ContentRewardChoiceItem1, "Interface\\Icons\\INV_Misc_Note_02");
  elseif (index == 6) then
    MQWizard_RewardChoiceGuildNone:SetChecked(nil);
    MQWizard_RewardChoiceGuildInvite:SetChecked(nil);
    --MQWizard_RewardChoiceGuildPromote:SetChecked(1);
    MyQuestsWizard_Page6ContentRewardChoiceItem1Name:SetText(MQL["Guild Promotion"]);
    SetItemButtonTexture(MyQuestsWizard_Page6ContentRewardChoiceItem1, "Interface\\Icons\\INV_Misc_Note_02");
  end
end

function MyQuests:RewardChoiceMoney_OnChange()
  self:Print("here");
  self:Print("value ", MoneyInputFrame_GetCopper(MyQuestsWizard_Page6ContentRewardChoiceMoney));
end
